<?php
class Admin_LeavedController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDid = empty($p['d_id'])?'':Tool_Fnc::safe_string($p['d_id']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
    
        $tDIMO = new DepartmentinfoModel;
        $tSMO = new SeniorModel;
        $tLMO = new LeaveModel;
        $tAMO = new AdminModel;
        $tWMO = new WorkerModel;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDepartmentID = empty($p['department_id'])?Tool_Fnc::ajaxMsg('系统错误'):Tool_Fnc::safe_string($p['department_id']);
            $tSql = 'select id,name from '.$tSMO->table.' where department = '.$pDepartmentID;
            $tDatas = $tSMO->query($tSql);
            print_r($tDatas);die;
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }

        $tWhere = ' 1 ';

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and s.department = '.$tAData[0]['department'];
            }
        }

        if(!empty($pDepartment)){
            $tDDatas = $tDIMO->field('num,introduce')->where('num='.$pDepartment)->fList();
            $tWhere .= ' and department = '.$pDepartment;
        }

        $tCount = $tLMO->field('count(id) c')->where($tWhere)->fList();
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        $tDatas = array();
        if(!empty($tCnt)){
            $tDatas = $tLMO->field('id,created leavetime,is_pay,leaved_reason,department')->where($tWhere)->order('id desc')->fList();
            if(!empty($tDatas)){
                foreach($tDatas as $tK=>$tV){
                    $tSql = 'select s.name name,s.created created from '.$tSMO->table.'  s where s.id = '.$tV['id'].' limit '.$tPage->limit();
                    $tSData = $tSMO->query($tSql); 
                    // print_r($tSql);
                    $tDatas[$tK]['name'] = $tSData[0]['name'];
                    $tDatas[$tK]['created'] = $tSData[0]['created'];
                    $tDIData = $tDIMO->field('introduce')->where('num = '.$tV['department'])->fRow();
                    $tDatas[$tK]['department'] = $tDIData['introduce'];
                }
            }
        }
        //所在部门
        // print_r($tDatas);
        $tDDatas = $tDIMO->field('num,introduce')->fList();

        $this->assign('pDepartment',$pDepartment);
        $this->assign('tDDatas',$tDDatas);
        $this->assign('tDatas',$tDatas);
    }
    public function addAction(){
        $p = $_REQUEST;
        
        $tSMO = new SeniorModel;
        $tLMO = new LeaveModel; 

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pName = empty($p['name'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['name']);
            $pLeavedreason = empty($p['leaved_reason'])?Tool_Fnc::ajaxMsg('离院原因不能为空'):Tool_Fnc::safe_string($p['leaved_reason']);
            $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);

            $tSmMO = new SettlementModel;
            $tSmData = $tSmMO->field('is_pay')->where('sid = '.$pName)->fRow();
            $tIspay = empty($tSmData['is_pay'])?2:1;

            $tTime = time();

            $tData = array(
                'sid' => $pName,
                'leaved_reason' => $pLeavedreason,
                'department' => $pDepartment,
                'is_pay' => $tIspay,
                'created' => $tTime, 
            );
            $tLMO->begin();
            if(!$tId = $tLMO->insert($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            // Tool_Fnc::ajaxMsg('操作失败',0,$tData);die;
            //更新老人表状态
            $tData = array(
                'id' => $pName,
                'leave_status' => 1,
                'updated' => $tTime,
            );
            if(!$tSMO->update($tData)){
                $tLMO->back();
                Tool_Fnc::ajaxMsg('操作失败 002',0,$tData);
            }
            $tLMO->commit();
            Tool_Fnc::ajaxMsg('操作成功',1,$tData);
        }
    }
}
?>
